package com.xhtt.modules.reportform.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.common.utils.DateUtils;
import com.xhtt.modules.reportform.dao.TttReportMapper;
import com.xhtt.modules.reportform.service.TttReportService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 统计分析-TTT
 * </p>
 *
 * @author xj
 * @since 2019-02-14
 */
@Service
public class TttReportServiceImpl extends ServiceImpl<TttReportMapper, Object> implements TttReportService {

    @Override
    public List<Map<String, Object>> totalDay(Map<String, Object> params) {

        LocalDateTime end = DateUtils.now();
        // 取前10天数据
        LocalDateTime start = end.plusDays(-10);

        Object startDate = params.get("startDate");
        Object endDate = params.get("endDate");
        if (startDate != null && endDate == null) {
            String startDateStr = startDate.toString();
            start = DateUtils.string2LocalDateTime(startDateStr, DateUtils.DATE_PATTERN);
            end = start.plusDays(10);
        }
        if (endDate != null && startDate == null) {
            String endDateStr = endDate.toString();
            end = DateUtils.string2LocalDateTime(endDateStr, DateUtils.DATE_PATTERN);
            start = end.plusDays(-10);
        }
        if (startDate == null || endDate == null) {
            params.put("startDate", start);
            params.put("endDate", end.plusDays(1));
        }

        return baseMapper.queryTotal(params);
    }

    @Override
    public List<Map<String, Object>> totalYear(Map<String, Object> params) {
        return baseMapper.queryTotal(params);
    }


    @Override
    public List<Map<String, Object>> item(Map<String, Object> params) {
        return baseMapper.queryItem(params);
    }

    @Override
    public List<Map<String, Object>> carDay(Map<String, Object> params) {

        Integer type = Integer.parseInt(params.get("type").toString());
        List<Map<String, Object>> result = new ArrayList<>();
        if (type.equals(1)) {
            result = baseMapper.queryCarDay(params);
        } else if (type.equals(2)) {
            int year = Integer.parseInt(params.get("year").toString());
            LocalDateTime start = LocalDateTime.of(year, 1, 1, 0, 0, 0);
            LocalDateTime end = LocalDateTime.of(year, 12, 31, 23, 59, 59);
            params.put("startDate", start.format(DateUtils.dtf));
            params.put("endDate", end.format(DateUtils.dtf));
            result = baseMapper.queryCarDay(params);
        }
        return result;
    }

}
